package com.simpleframe.persistence.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;


import com.simpleframe.exception.SimpleframeException;
import com.simpleframe.util.Utils;

public class Searchers {
	
	private List<Condition> conditions;
	private Searcher searcher;
	
	private Searchers(){
		conditions = new ArrayList<Condition>();
	}
	
	public static Searchers create(Searcher searcher){
		if(searcher==null){
			throw new SimpleframeException("Finder 不是 searcher 所建， 不能创建搜索 ");
		}
		Searchers searchers = new Searchers();
		searchers.searcher = searcher;
		return searchers;
	}
	
	public List<Condition> getConditions(){
		return conditions;
	}
	
	public void andEq(String name){
		add("and", name, "=", searcher.getValue(name));
	}
	
	public void andLike(String name){
		System.out.println(searcher+"{{{{{{{{{{{{{");
		add("and", name, "like", searcher.getValue(name));
	}
	
	/**
	 * 大于 > (gt----->greater than)
	 */
	public void andGt(String name){
		add("and", name, ">", searcher.getValue(name));
	}
	
	/**
	 * 大于 > (gt----->greater than)
	 */
	public void andGt(String name, Object value){
		add("and", name, ">", value);
	}
	
	/**
	 * 大于 >= (ge----->greater equal)
	 */
	public void andGe(String name){
		add("and", name, ">=", searcher.getValue(name));
	}
	
	/**
	 * 大于 >= (ge----->greater equal)
	 */
	public void andGe(String name, Object value){
		add("and", name, ">=", value);
	}
	
	/**
	 * 小于< (Lt---->less than)
	 */
	public void andLt(String name){
		add("and", name, "<", searcher.getValue(name));
	} 
	
	/**
	 * 小于< (Lt---->less than)
	 */
	public void andLt(String name, Object value){
		add("and", name, "<", value);
	} 
	
	/**
	 * 小于等于<= (le---->less equal)
	 */
	public void andLe(String name){
		add("and", name, "<=", searcher.getValue(name));
	}
	
	public void andStartEndDate(String name){
		andStartEndDate(name, searcher.getStartDate(), searcher.getEndDate());
	}
	
	/**
	 * 加入大于某时间小时某时间的条件
	 * @param sn 开始时间字段
	 * @param en 结果时间字段
	 * @param sd 开始时间
	 * @param ed 结果时间
	 */
	public void andStartEndDate(String sn, String en, Date sd, Date ed){
		this.andGe(sn, sd);
		this.andLt(en, getEndtime(sd, ed));
	}
	
	/**
	 * 加入大于某时间小时某时间的条件
	 * @param n
	 * @param sd
	 * @param ed
	 */
	public void andStartEndDate(String n, Date sd, Date ed){
		andStartEndDate(n, n, sd, ed);
	}
	
	private Date getEndtime(Date d1,Date d2){
		Date endTime = d2;
		if(d2!=null){
			//if(d1.getTime() == d2.getTime()){
				endTime = DateUtils.addDays(d2, 1);
			//}
		}
		return endTime;
	}
	
	public boolean validate(Object value){
		if(Utils.isNull(value)){
			return true;
		}
		
		String strv = String.valueOf(value);
		if(Utils.isEmpty(strv)){
			return true;
		}
		if("0".equals(strv)){
			return true;
		}
		return false;
	}
	
	public void add(String logic, String name, String Operation, Object value){
		if(value==null){
			return;
		}
		Condition con = new Condition();
		con.setLogic(logic);
		con.setField(name);
		con.setOperation(Operation);
		con.setValue(value);
		conditions.add(con);
	}
	
}
